﻿Public Class BLLAgendamento
    Private Nome As String
    Private curso As String
    Private Codigo As Integer
    Private agecodigo As Integer
    Private pescodigo As Integer
    Private codsala As Integer
    Private data As String
    Private hinicial As DateTime
    Private hfinal As DateTime
    Private usuario As Integer
    Private lRecurso As New List(Of BLLItensRecurso)
    Private objCliente As BLLSolicitante

    Dim objDAO As DALAgendamento
#Region "Propriedades"
    Public Property pcodsala As Integer
        Get
            Return Me.codsala
        End Get
        Set(ByVal value As Integer)
            Me.codsala = value
        End Set
    End Property
    Public Property pagecodigo As Integer
        Get
            Return Me.agecodigo
        End Get
        Set(ByVal value As Integer)
            Me.agecodigo = value
        End Set
    End Property
    Public Property ppescodigo As Integer
        Get
            Return Me.pescodigo
        End Get
        Set(ByVal value As Integer)
            Me.pescodigo = value
        End Set
    End Property
    Public Property pcurso() As String
        Get
            Return Me.curso
        End Get
        Set(ByVal value As String)
            Me.curso = value
        End Set
    End Property

    Public Property pobjCliente As BLLSolicitante
        Get
            Return Me.objCliente
        End Get
        Set(ByVal value As BLLSolicitante)
            Me.objCliente = value
        End Set
    End Property

    Public Property plRecurso As List(Of BLLItensRecurso)
        Get
            Return lRecurso
        End Get
        Set(ByVal value As List(Of BLLItensRecurso))
            Me.lRecurso = value
        End Set
    End Property
    Public Property pdata As String
        Get
            Return Me.data
        End Get
        Set(ByVal value As String)
            Me.data = value
        End Set
    End Property
    Public Property phfinal As DateTime
        Get
            Return Me.hfinal
        End Get
        Set(ByVal value As DateTime)
            Me.hfinal = value
        End Set
    End Property
    Public Property pusuario As Integer
        Get
            Return Me.usuario
        End Get
        Set(ByVal value As Integer)
            Me.usuario = value
        End Set
    End Property

    Public Property phinicial As DateTime
        Get
            Return Me.hinicial
        End Get
        Set(ByVal value As DateTime)
            Me.hinicial = value
        End Set
    End Property
    Public Property pNome() As String
        Get
            Return Me.Nome
        End Get
        Set(ByVal value As String)
            Me.Nome = value
        End Set
    End Property

    Public Property pCodigo() As Integer
        Get
            Return Me.Codigo
        End Get
        Set(ByVal value As Integer)
            Me.Codigo = value
        End Set
    End Property
#End Region
#Region "Métodos"
    Sub excluirTodosItensRecurso()   'OK
        Dim obj As BLLItensRecurso
        Try
            obj = New BLLItensRecurso
            obj.pitecodigo = Me.agecodigo
            obj.excluirTodos()
        Catch ex As Exception
            Throw ex
        Finally
            obj = Nothing
        End Try
    End Sub
    Function excluir() As Boolean
        If (Me.pagecodigo <> 0) Then
            Dim ret As Boolean
            Dim retlinhas As Integer
            Try
                objDAO = New DALAgendamento
                excluirTodosItensRecurso()
                retlinhas = objDAO.excluir(Me.pagecodigo)
                If (retlinhas > 0) Then
                    ret = True
                Else
                    ret = False
                End If
            Catch ex As Exception
                Throw ex
                ret = False
            Finally
                objDAO = Nothing
            End Try
            Return ret
        Else
            Return False
        End If
    End Function
    Sub insereItemRecurso()  'OK
        Try
            For Each item As BLLItensRecurso In lRecurso
                item.pitecodigo = Me.agecodigo
                item.inserir()
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Function alterar() As Boolean
        Dim ret As Boolean
        Try
            objDAO = New DALAgendamento
            objDAO.alterar(Me.data, Me.usuario, Me.codsala, Me.hinicial, Me.hfinal, Me.curso, objCliente.pCodigo, objCliente.ppessoa, Me.agecodigo)
            If (Me.agecodigo > 0) Then
                excluirTodosItensRecurso()
                insereItemRecurso()
                ret = True
            Else
                ret = False
            End If
        Catch ex As Exception
            Throw ex
            ret = False
        Finally
            objDAO = Nothing
        End Try
        Return ret

    End Function

    Function LocalizaAgendamento() As DataTable
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaAgendamento()
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function LocalizaAgendamento(ByVal codigo As Integer) As DataTable
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaAgendamento(codigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function LocalizaRecurso(ByVal codigo As Integer, ByVal data As String) As DataTable
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaRecurso(codigo, data)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function inserir() As Boolean

        If (Me.objCliente.pnome <> "") Then
            Dim ret As Boolean
            Try
                objDAO = New DALAgendamento
                Me.pagecodigo = objDAO.inserir(Me.data, Me.usuario, Me.codsala, Me.hinicial, Me.hfinal, Me.curso, objCliente.pCodigo, objCliente.ppessoa)
                insereItemRecurso()
                ret = True
            Catch ex As Exception
                Throw ex
                ret = False
            Finally
                objDAO = Nothing
            End Try
            Return ret
        Else
            Return False
        End If
    End Function
    Sub carregaTodosItemRecurso()
        Dim obj As BLLItensRecurso
        Dim tab As DataTable
        Try
            obj = New BLLItensRecurso
            obj.pitecodigo = Me.agecodigo
            tab = obj.localizarTodos()
            For Each linha As DataRow In tab.Rows
                'crei objeto
                obj = New BLLItensRecurso
                'carregar com propriedades
                obj.pitecodigo = linha("ite_codigo").ToString
                obj.pobjRecurso = New BLLRecurso(linha("rec_codigo").ToString)

                'adicionar na lista
                lRecurso.Add(obj)

            Next
        Catch ex As Exception
            Throw ex
        Finally
            obj = Nothing
        End Try
    End Sub
    Private Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.localiza(codigo)
            If (tab.Rows.Count > 0) Then
                Me.agecodigo = tab.Rows(0).Item("age_codigo").ToString
                'Me.objCliente = New BBLCliente(tab.Rows(0).Item("pes_codigo").ToString)
                Me.data = tab.Rows(0).Item("age_data").ToString
                Me.pescodigo = tab.Rows(0).Item("pes_codigo").ToString
                Me.hinicial = tab.Rows(0).Item("age_horainicial").ToString
                Me.hfinal = tab.Rows(0).Item("age_horafinal").ToString
                Me.curso = tab.Rows(0).Item("age_curso").ToString
                Me.usuario = tab.Rows(0).Item("usu_codigo").ToString
                carregaTodosItemRecurso()
            Else
                limpa()
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
    End Sub
    Sub New()

    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub limpa()
        Me.Codigo = Nothing
        Me.data = Nothing
        Me.hfinal = Nothing
        Me.hinicial = Nothing
        Me.Nome = Nothing
        Me.usuario = Nothing
        Me.objCliente = Nothing
        lRecurso.Clear()
    End Sub
    Function LocalizarPessoa(ByVal nome As String, ByVal descricao As String) As DataTable
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaPessoa(nome, descricao)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
#Region "Professor"
    Function LocalizaProfessor(ByVal Codigo As Integer)
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaProfessor(codigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function LocalizaProfessor(ByVal Nome As String)
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaProfessor(nome)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function LocalizaProfessorMatricula(ByVal Matricula As String)
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaProfessorMatricula(Matricula)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
#End Region
#Region "Aluno"
    Function LocalizaAluno(ByVal Codigo As Integer)
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaAluno(Codigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function LocalizaAluno(ByVal Nome As String)
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaAluno(Nome)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function LocalizaAlunoRA(ByVal RA As String)
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaAlunoRA(RA)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
#End Region
#Region "Funcionário"
    Function LocalizaFuncionario(ByVal Codigo As Integer)
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaFuncionario(Codigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function LocalizaFuncionario(ByVal Nome As String)
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaFuncionario(Nome)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function LocalizaFuncionarioMatricula(ByVal Matricula As String)
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaFuncionarioMatricula(Matricula)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
#End Region
#Region "Sala"
    Function LocalizarSala(Filtro As Integer, ByVal Descricao As String) As DataTable
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaSala(Filtro, Descricao)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function LocalizarSala(ByVal codigo As Integer) As DataTable
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaSala(codigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
#End Region
    Function LocalizarPessoa(ByVal codigo As Integer, ByVal descricao As String) As DataTable
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaPessoa(codigo, descricao)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function

    Function LocalizarRecurso(ByVal nome As String) As DataTable
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaRecurso(nome)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function LocalizarRecurso(ByVal codigo As Integer) As DataTable
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.LocalizaRecurso(codigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function ValidaSala(ByVal dia As String, ByVal hinico As DateTime, ByVal hfinal As DateTime, ByVal codsala As Integer) As Boolean
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.ValidaSala(dia, hinico, hfinal, codsala)
            For Each linha As DataRow In tab.Rows
                linha("sal_sala").ToString()
                Return False
            Next
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return True
    End Function
    Function ValidaRecurso(ByVal data As String, ByVal codrec As Integer, ByVal hinico As DateTime, ByVal hfinal As DateTime) As Boolean
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.ValidaRecurso(data, codrec, hinico, hfinal)
            For Each linha As DataRow In tab.Rows
                linha("age_data").ToString()
                Return False
            Next
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return True
    End Function
    Function ValidaInsere(ByVal data As String, ByVal salcod As Integer, ByVal hinico As DateTime, ByVal hfinal As DateTime) As Boolean
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.ValidaInsere(data, salcod, hinico, hfinal)
            For Each linha As DataRow In tab.Rows
                linha("sal_codigo").ToString()
                Return False
            Next
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return True
    End Function
    Function ValidaAlterar(ByVal data As String, ByVal salcod As Integer, ByVal hinico As DateTime, ByVal hfinal As DateTime, ByVal age_codigo As Integer) As Boolean
        Dim tab As DataTable
        Try
            objDAO = New DALAgendamento
            tab = objDAO.ValidaAlterar(data, salcod, hinico, hfinal, age_codigo)
            For Each linha As DataRow In tab.Rows
                linha("sal_codigo").ToString()
                Return False
            Next
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return True
    End Function
#End Region


End Class
